package com.kxq.zhuoyue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kxq.zhuoyue.po.ClientHistory;
import com.kxq.zhuoyue.po.Clients;
import com.kxq.zhuoyue.service.ClientHistoryService;
import com.kxq.zhuoyue.service.ClientService;
import com.kxq.zhuoyue.util.result.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/client")
public class ClientController {

    @Autowired
    private ClientService clientService;

    @Autowired
    private ClientHistoryService clientHistoryService;

    @GetMapping("/findAll")
    public AjaxResult findAll(){
        List<Clients> list = clientService.list();
        return AjaxResult.success("查询数据为",list);
    }

    @DeleteMapping("/deleteClients/{list}")
    public AjaxResult deleteClients(@PathVariable List list){
        boolean b = clientService.removeByIds(list);
        return AjaxResult.success(b);
    }
    @PostMapping("/insClients")
    public AjaxResult insClients(@RequestBody Clients clients){
        boolean save = clientService.save(clients);
        return AjaxResult.success(save);
    }

    @PutMapping("/updClients")
    public AjaxResult updClient(@RequestBody Clients clients){
        boolean b = clientService.updateById(clients);
        return AjaxResult.success(b);
    }


    @PostMapping("/findByPage")
    public AjaxResult findByPage(@RequestBody Clients clients){
        Page<Clients> page = new Page<>(clients.getPageNum(),clients.getPageSize());
        IPage<Clients> ipage = clientService.page(page);
        List<Clients> list = ipage.getRecords();
        return AjaxResult.success(list);
    }
    @PostMapping("/findByLabelLike")
    public AjaxResult findByNameLike(@RequestBody Clients clients){
        QueryWrapper<Clients> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("content_label",clients.getContentLabel());
        List<Clients> list = clientService.list(queryWrapper);
        return AjaxResult.success(list);
    }
    @PostMapping("/findByStatusLike")
    public AjaxResult findByStatusLike(@RequestBody Clients clients){
        QueryWrapper<Clients> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("client_status",clients.getClientStatus());
        List<Clients> list = clientService.list(queryWrapper);
        return AjaxResult.success(list);
    }

    @PostMapping("/getUserList/{ClientId}")
    public AjaxResult getUserList(@PathVariable String ClientId){
        List<Clients> list = clientService.getList(ClientId);
        return AjaxResult.success("获取客户统计",list);
    }


    /**
     * 根据id综合查询
     * @param clientId
     * @return
     */
    @PostMapping("/getById/{clientId}")
    public AjaxResult getById(@PathVariable String clientId){

        Clients byId = clientService.getById(clientId);
        return AjaxResult.success(byId);
    }

    /**
     * 移除黑名单
     * @param clientId
     * @return
     */
    @GetMapping("/deleteBack/{clientId}")
        public AjaxResult deleteBack(@PathVariable String clientId){
            LambdaUpdateWrapper<Clients> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(Clients::getClientType,1);
            updateWrapper.eq(Clients::getClientId,clientId);
        boolean update = clientService.update(updateWrapper);
        LambdaQueryWrapper<ClientHistory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ClientHistory::getClientId,clientId);
        boolean remove = clientHistoryService.remove(queryWrapper);
        if (update || remove){
            return AjaxResult.success("移除成功 ！");
        } else {
            return  AjaxResult.success("移除失败 ！");
        }
    }
}
